package cn.com.anysdk.starter.config.map;

import cn.com.anysdk.map.config.AmapConfig;
import cn.com.anysdk.map.factory.MapServiceFactory;
import cn.com.anysdk.map.service.MapService;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.util.Timeout;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 地图服务自动配置类
 */
@Configuration
@EnableConfigurationProperties({AmapProperties.class})
public class MapAutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "anysdk.map.amap", name = "enabled", havingValue = "true")
    @ConditionalOnMissingBean
    public MapService amapService(AmapProperties properties) {
        AmapConfig config = new AmapConfig();
        config.setEnabled(properties.isEnabled());
        config.setAk(properties.getAk());
        config.setSk(properties.getSk());
        config.setTimeout(Timeout.ofMilliseconds(properties.getTimeout()));
        config.setRetryTimes(properties.getRetryTimes());
        config.setRetryInterval(properties.getRetryInterval());

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(100);
        connectionManager.setDefaultMaxPerRoute(20);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .build();

        return MapServiceFactory.createMapService(config);
    }
} 